package com.ylfstu.Homework;

import java.util.*;

public class Homework01new {
    public static void main(String[] args) {
        Dao<User> userDao = new Dao<>();
        User katy = new User(1, 23, "katy");
        User jack = new User(2, 25, "jack");
        User lance = new User(3, 28, "lance");
        userDao.save(Integer.valueOf(katy.getId()).toString(), katy);
        userDao.save(Integer.valueOf(jack.getId()).toString(), jack);
        userDao.save(Integer.valueOf(lance.getId()).toString(), lance);
        System.out.println(userDao.getHashMap());
        System.out.println(userDao.get(String.valueOf(2)));
        User rabbit = new User(6, 30, "rabbit");
        userDao.update(String.valueOf(3), rabbit);
        System.out.println(userDao.getHashMap());
        System.out.println(userDao.list());
        userDao.delete(String.valueOf(1));
        System.out.println(userDao.getHashMap());
    }
}
class Dao<T> {
    private Map<String, T> hashMap = new HashMap<>();

    public Map<String, T> getHashMap() {
        return hashMap;
    }

    public void save(String id, T entity) {
        hashMap.put(id, entity);
    }

    public T get(String id) {
        return hashMap.get(id);
    }

    public void update(String id, T entity) {
        hashMap.replace(id, entity);
    }

    public List<T> list() {
        List<T> list = new ArrayList<>();
        Set<String> keySet = hashMap.keySet();
        for (String s : keySet) {
            list.add(hashMap.get(s));
        }
        return list;
    }

    public void delete(String id) {
        hashMap.remove(id);
    }
}

class User{
    private int id;
    private int age;
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User() {
    }
}
